package com.jwgf.smarowindingmachine_unity.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.jwgf.common.IPConfig;
import com.jwgf.common.ViewCache;
import com.jwgf.smarowindingmachine_unity.R;

import java.util.regex.Pattern;

public class EquipmentMaintenanceNetworkSettingFragment extends BaseFragment implements TextView.OnEditorActionListener {
    public static final String fragmentTag = "EquipmentMaintenanceNetworkSettingFragment";
    private View v;
    private EditText localIP;
    private EditText machineID;
    private EditText serverIP;
    private EditText serverPort;
    private static String ipRegex = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = ViewCache.getInstance().getView(R.layout.fragment_equipment_maintenance_network_setting);
        initViews();
        updateViews();
        return v;
    }

    private void initViews() {
        localIP = v.findViewById(R.id.ip_input);
        localIP.setOnEditorActionListener(this);
        localIP.setSelectAllOnFocus(true);
        localIP.setEnabled(false);

        machineID = v.findViewById(R.id.machine_id_input);
        machineID.setOnEditorActionListener(this);
        machineID.setSelectAllOnFocus(true);

        serverIP = v.findViewById(R.id.server_ip_input);
        serverIP.setOnEditorActionListener(this);
        serverIP.setSelectAllOnFocus(true);

        serverPort = v.findViewById(R.id.server_port_input);
        serverPort.setOnEditorActionListener(this);
        serverPort.setSelectAllOnFocus(true);
    }

    public void updateViews() {
        localIP.setText(IPConfig.getIp());
        machineID.setText(IPConfig.getMachineID());
        serverIP.setText(IPConfig.getServerIp());
        serverPort.setText(IPConfig.getPort());
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            if (TextUtils.isEmpty(v.getText().toString())) {
                return false;
            }
            String hint = null;
            switch (v.getId()) {
                case R.id.ip_input:
                    if (Pattern.matches(ipRegex, v.getText())) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            if (Settings.System.canWrite(getActivity())) {
                                // Do stuff here
                                IPConfig.setIp(v.getText().toString());
                            }
                            else {
                                Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                                intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }
                        }
                    } else {
                        hint = "本机IP地址格式不正确";
                    }
                    break;
                case R.id.machine_id_input:
                    IPConfig.setMachineID(v.getText().toString());
                    break;
                case R.id.server_ip_input:
                    if (Pattern.matches(ipRegex, v.getText())) {
                        IPConfig.setServerIp(v.getText().toString());
                    } else {
                        hint = "服务器IP地址格式不正确";
                    }
                    break;
                case R.id.server_port_input:
                    int port = Integer.valueOf(v.getText().toString());
                    if (port > 1024 && port <= 65535) {
                        IPConfig.setPort(v.getText().toString());
                    } else {
                        hint = "服务器端口无效";
                    }
                    break;
            }
            if (hint != null) {
                WarningDialog.show(getActivity(), hint);

            }
        }
        return false;
    }

}
